<template>
	<view class="viewport">
		<view class="logo">
			<image
				src="https://cdn7.axureshop.com/demo2024/2316761/images/%E4%BA%A7%E5%93%81%E6%A6%82%E8%BF%B0/u49.png">
			</image>
			<view>
				<text>星星文旅民宿</text>
			</view>
		</view>
		<view class="login">
			<view class="uni-form-item uni-column">
				<uni-section>
					<uni-easyinput type="text" v-model="phoneInput" @input="validatePhone" placeholder="输入手机号码"></uni-easyinput>
					<text v-if="phoneError" class="error-message">{{ phoneError }}</text>
				</uni-section>
				<uni-section>
					<uni-easyinput type="password" v-model="passwordInput" placeholder="登录密码"></uni-easyinput>
				</uni-section>
			</view>
			<view class="zi">
				<view @click="register">注册</view>
				<view @click="forgot">忘记密码</view>
			</view>
			<button class="button phone" @click="dologin" :disabled="flg">
				<text>登录</text>
			</button>
			<view class="extra">
				<view class="options">
					<view class="tips">
						<radio-group @change="check">
							<label class="btn">
								<radio value="cb" class="check" />
								<text>登录确认同意《用户协议》和《隐私政策》</text>
							</label>
						</radio-group>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref, watch } from 'vue';

	let flg = ref(true);
	const phoneInput = ref<string>('');
	const passwordInput = ref<string>('');
	const phoneError = ref<string>('')
	const isValidPhone = ref<boolean>(true)
	const check = (e) => {
		if (e.detail.value.length > 0) {
			flg.value = false;
		} else {
			flg.value = true;
		}
	};

	const register = () => {
		uni.navigateTo({
			url: '/pages/register/register'
		});
	};
	const validatePhone = (value : string) => {
		const phoneRegex = /^1[3-9]\d{9}$/;
		if (value && !phoneRegex.test(value)) {
			phoneError.value = '请输入有效的手机号码';
			isValidPhone.value = false;
		} else {
			phoneError.value = '';
			isValidPhone.value = true;
		}
	}

	watch(phoneInput, validatePhone);
	const forgot = () => {
		uni.navigateTo({
			url: `/pages/forgot/forgot`
		});
	};

	const dologin = () => {
		uni.request({
			url: 'http://127.0.0.1:3000/ljy/login',
			method: 'GET',
			data: {
				phone: phoneInput.value,
				password: passwordInput.value
			},
			success(res : any) {
				console.log(res.data.data);

				if (res.data.code == 200) {
					uni.showToast({
						title: '登录成功',
						icon: 'success',

					});
					uni.reLaunch({
						url: '/pages/index/index'
					});
					localStorage.setItem('data', JSON.stringify(res.data.data))
				} else {
					uni.showToast({
						title: '登录失败',
						icon: 'error',
						content: res.data.message,
						showCancel: false
					});
				}
			},
			fail(error : any) {
				console.error('请求失败:', error);
				uni.showToast({
					title: '请求失败',
					duration: 2000,
					icon: 'none'
				});
			}
		});
	};
</script>

<style scoped lang="scss">
	.viewport {
		display: flex;
		flex-direction: column;
		height: 100%;
		padding: 20rpx 40rpx;
	}

	.logo {
		flex: 1;
		text-align: center;

		image {
			width: 452rpx;
			height: 256rpx;
			margin-top: 100rpx;
		}
	}

	.login {
		display: flex;
		flex-direction: column;
		height: calc(100vh - 200rpx);
		padding: 40rpx 20rpx 20rpx;

		.zi {
			display: flex;
			justify-content: space-between;
			color: skyblue;
		}

		.uni-form-item {
			display: flex;
			flex-direction: column;
			margin-bottom: 20rpx;

			.uni-input {
				width: 100%;
				height: 80rpx;
				font-size: 28rpx;
				border-radius: 72rpx;
				border: 1px solid #ddd;
				margin-bottom: 30rpx;
				padding-left: 30rpx;
			}
		}

		.button {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 80%;
			height: 80rpx;
			font-size: 28rpx;
			border-radius: 72rpx;
			color: #fff;
			background-color: blue;
			margin-top: 20px;
		}

		.extra {
			padding-top: 30rpx;
			display: flex;
			align-items: center;

			.options {
				.tips {
					.btn {
						.check {
							width: 40rpx;
							height: 20rpx;
							padding-right: 20rpx;
						}

						text {
							font-size: 30rpx;
						}
					}
				}
			}
		}
	}
	.error-message {
		color: red;
		font-size: 25rpx;
		margin-top: 10rpx;
	}
</style>